package com.htzy.extrace.ui.domain;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import com.htzy.extrace.misc.model.ExpressSheet;
import com.htzy.extrace.net.IDataAdapter;
import com.htzy.extrace.ui.main.ExTraceApplication;
import com.htzy.extrace.ui.main.R;
import com.htzy.extrace.util.Global;

public class ExpressListAdapter extends ArrayAdapter<ExpressSheet> implements
		IDataAdapter<List<ExpressSheet>> {

	private List<ExpressSheet> itemList;
	private Context context;
	private String ex_type;

	private static ArrayList<String> telNumbers;

	public ExpressListAdapter(List<ExpressSheet> itemList, Context ctx,
			String ex_type) {
		super(ctx, R.layout.express_list_item, itemList);
		telNumbers = new ArrayList<String>();
		this.itemList = itemList;
		this.context = ctx;
		this.ex_type = ex_type;
	}

	public int getCount() {
		if (itemList != null)
			return itemList.size();
		return 0;
	}

	public ExpressSheet getItem(int position) {
		if (itemList != null)
			return itemList.get(position);
		return null;
	}

	public long getItemId(int position) {
		if (itemList != null)
			return itemList.get(position).hashCode();
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		View v = convertView;
		hold hd = null;
		if (v == null) {
			hd = new hold();
			LayoutInflater inflater = (LayoutInflater) context
					.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
			v = inflater.inflate(R.layout.express_list_item, null);
			hd.name = (TextView) v.findViewById(R.id.name);
			hd.telCode = (TextView) v.findViewById(R.id.tel);
			hd.address = (TextView) v.findViewById(R.id.addr);
			hd.time = (TextView) v.findViewById(R.id.time);
			hd.status = (TextView) v.findViewById(R.id.st);

			v.setTag(hd);
		} else {
			hd = (hold) v.getTag();
		}

		ExpressSheet es = getItem(position);
		switch (ex_type)
			{
			case "ExDLV": // 派送
				// 批量拨打电话
				if (es.getRecever() != null) {
					String tel = es.getRecever().getTelCode();
					if (ExTraceApplication.isISDEUBG()) {
						telNumbers.add("10086");
					} else {
						telNumbers.add(tel);
					}
					hd.name.setText(es.getRecever().getName()); // 接收者姓名
					hd.telCode.setText(es.getRecever().getTelCode()); // 接收者电话
					hd.address.setText(es.getRecever().getAddress()); // 接收者
				}
				if (es.getAccepterTime() != null) {
					// SimpleDateFormat myFmt=new
					// SimpleDateFormat("MM月dd日 HH:mm");
					// hd.time.setText(DateFormat.format("MM月dd日 HH:mm",es.getAccepterTime()));
					hd.time.setText(new SimpleDateFormat("MM月dd日 HH:mm")
							.format(es.getAccepterTime()));
				}
				break;
			case "ExRCV": // 揽收
				if (es.getSender() != null) {
					hd.name.setText(es.getSender().getName()); // 发送者姓名
					hd.telCode.setText(es.getSender().getTelCode());
					hd.address.setText(es.getSender().getAddress());
				}
				if (es.getAccepterTime() != null) {
					// SimpleDateFormat myFmt=new
					// SimpleDateFormat("MM月dd日 hh:mm");
					// hd.time.setText(DateFormat.format("MM月dd日 hh:mm",es.getAccepterTime()));
					hd.time.setText(new SimpleDateFormat("MM月dd日 HH:mm")
							.format(es.getAccepterTime()));
				}
				break;
			case "ExTAN": // 这个需要改
				if (es.getRecever() != null) {
					hd.name.setText(es.getRecever().getName()); // 接收者姓名
					hd.telCode.setText(es.getRecever().getTelCode()); // 接收者电话
					hd.address.setText(es.getRecever().getAddress()); // 接收者
				}
				if (es.getAccepterTime() != null) {
					/*
					 * SimpleDateFormat myFmt=new
					 * SimpleDateFormat("MM月dd日 hh:mm");
					 * hd.time.setText(myFmt.format(es.getAccepterTime()));
					 */
					hd.time.setText(new SimpleDateFormat("MM月dd日 HH:mm")
							.format(es.getAccepterTime()));
				}
				break;
			}

		String stText = "";
		switch (es.getStatus())
			{
			case ExpressSheet.STATUS.STATUS_CREATED:
				stText = "创建";
				break;
			case ExpressSheet.STATUS.STATUS_RECEIVED:
				stText = "收件";
				break;
			case ExpressSheet.STATUS.STATUS_DELIVERIED:
				stText = "交付";
				break;
			}
		hd.status.setText(stText);
		return v;
	}

	@Override
	public List<ExpressSheet> getData() {
		return itemList;
	}

	@Override
	public void setData(List<ExpressSheet> data) {
		this.itemList = data;
	}

	private class hold {
		TextView name;
		TextView telCode;
		TextView address;
		TextView time;
		TextView status;
	}
}